import { GameEngine } from "./gameEngine";

export class Enemys {
  public id: number = 0;
  public el: HTMLDivElement;
  public hp: number = 2;
  public timer: number = 0;
  constructor() {
    this.id = parseInt(Math.random() * 1000000 + "");
    this.el = document.createElement("div");
    this.el.className = "enemy-small";
    GameEngine.instance().el.appendChild(this.el);
    GameEngine.instance().enemys[this.id] = this;
    let pX = GameEngine.instance().el.offsetWidth * Math.random();
    pX -= this.el.offsetWidth;
    if (pX <= 0) {
      pX = 0;
    }

    this.el.style.left = parseInt(pX + "") + "px";
    this.el.style.top = 0 + "px";

    this.move();
  }
  move() {
    let step: number = 10;
    this.timer = setInterval(() => {
      this.el.style.top = this.el.offsetTop + step + "px";
      if (this.el.offsetTop > GameEngine.instance().el.offsetHeight) {
        clearInterval(this.timer);
      }
    }, 100);
  }
  boom() {
    let num: number = 0;
    let timer = setInterval(() => {
      num++;
      this.el.style.backgroundImage = `url('/src/assets/images/plane1_die${num}.png')`;
      if (num >= 3) {
        clearInterval(timer);
        GameEngine.instance().enemys.splice(this.id, 1);
        this.el.remove();
      }
    }, 80);
  }
  hurt() {
    this.hp--;
    if (this.hp <= 0) {
    //   clearInterval(this.timer);
      this.boom();
    }
  }
}
